package com.witdata.common.config;

import io.minio.MinioClient;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

@Data
@Configuration
@Slf4j
@ConfigurationProperties(prefix = "minio")
public class MinioConfiguration {

    private String url;

    /**
     * 访问地址
     */
    private String endpoint;

    /**
     * accessKey类似于用户ID，用于唯一标识你的账户
     */
    private String accessKey;

    /**
     * secretKey是你账户的密码
     */
    private String secretKey;

    /**
     * 默认存储桶
     */
    private String bucketName;


    @PostConstruct
    public void setUrl(){
        if(StringUtils.hasText(endpoint) && StringUtils.hasText(bucketName)){
            this.url = endpoint + "/" + bucketName;
        }else{
            this.url = "";
        }
    }


    @Bean
    public MinioClient minioClient() {
        MinioClient minioClient = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
        log.info("MinioClient initialized");
        return minioClient;
    }

}
